package com.etong.frame.directive.impl.database;

import java.util.Map;

import com.etong.frame.directive.util.CommonStatus;

public class DatabaseCount extends DatabaseDirective {

	String countBuilder;

	public DatabaseCount(Statement statement) {
		super(statement);
	}

	@Override
	public boolean buildConst(Statement op) {

		StringBuilder tempBuilder = new StringBuilder(100);

		tempBuilder.append("Select count(*) from ").append(
				getStatement().getTable_name());

		String whereExpr = getStatement().getWhere_expr();
		if (null != whereExpr && !whereExpr.isEmpty()) {
			// 创建Select语句变量
			addPlaceholders(createPlaceholders(whereExpr, true));
			tempBuilder.append(" where ").append(whereExpr);
		}

		logger.info("Build count:" + getStatement().getName() + "->"
				+ tempBuilder.toString());

		countBuilder = tempBuilder.toString();

		return true;
	}

	@Override
	protected CommonStatus buildVariable(Map<String, Object> vars) {
		CommonStatus status = new CommonStatus();
		status.setObject(countBuilder.toString());
		return status;
	}

	public String getStatementConfig() {
		return countBuilder.toString();
	}
}
